package com.yanqu.road.web.mgr;

import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.net.ServerNetConfig;
import com.yanqu.road.utils.net.ServerNetConfigHelper;
import com.yanqu.road.utils.ServerType;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.URL;

public class WebServiceMgr extends TempMgr {

	private static Logger logger = LogManager.getLogger(Class.class.getName());

	private static GameWebServiceInvoke webServiceInvoke;
	private static String ipAddress;
	private static int port;

	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean reloadConfig() throws Exception {
		return true;
	}

	private static boolean initWebServiceInvoke() {
		if (WebServer.getInstance().isUserCenter()) {
			return true;
		}
		ServerNetConfig netConfig = ServerNetConfigHelper.getServerNetConfig(ServerType.SERVER_WEB, 0);
		try {
			webServiceInvoke = GameWebServiceInvoke.getInstance(new URL("http://" + netConfig.getAddress() + ":" + netConfig.getPort() + "/gamews?wsdl"));
			ipAddress = netConfig.getAddress();
			port = netConfig.getPort();
			return true;
		} catch (Exception e) {
			logger.error("initWebServiceInvoke exception, ip {} ,port {}" ,netConfig.getAddress() ,netConfig.getPort(), e);
		}
		return false;
	}

	public static GameWebServiceInvoke getWebServiceInvoke() {
		if (webServiceInvoke == null) {
			initWebServiceInvoke();
		}
		return webServiceInvoke;
	}

	@Override
	public boolean reloadData() {
		if (WebServer.getInstance().getServerId() > 0) {
			return initWebServiceInvoke();
		}
		return true;
	}

	@Override
	public boolean stop() {
		return true;
	}
}
